package com.synjones.zeuscloudweb.service.impl;

import com.synjones.zeus.model.web.AdminBean;
import com.synjones.zeuscloudweb.dao.AdminDaoMapper;
import com.synjones.zeuscloudweb.dao.RoleDaoMapper;
import com.synjones.zeuscloudweb.service.AdminService;
import com.synjones.zeuscloudweb.util.ccl.datastructure.impl.BaseDto;
import com.synjones.zeuscloudweb.util.ccl.util.G4Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author  yys
 * Description:系统用户管理ServiceImpl
 * @date  created in 15:13 2018-01-08
 * Modified by:
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDaoMapper adminDaoMapper;
    @Resource
    private RoleDaoMapper roleDaoMapper;
    /**
     *  查询注册帐户名是否被占用
     *  @param username 注册用户名
     *  @author yys
     *  Description:  查询注册帐户名是否被占用
     *  @date  15:24 2018-01-08
     *  @return  int
     *  throws
     */
    @Override
    public int selectCountAdminByAdminName(String username) {
        return adminDaoMapper.selectCountAdminByAdminName(username);
    }
    /**
     *  管理员添加
     *  @param  adminBean 用户实体
     *  @param  roleId 角色ID
     *  @author yys
     *  Description:  管理员添加
     *  @date  16:23 2018-01-08
     *  @return  null
     *  throws
     */
    @Override
    @Transactional
    public BaseDto insertAdmin(AdminBean adminBean , String roleId) {
        BaseDto jsonDto = new BaseDto();
        try{
            //先增加的管理员
            adminDaoMapper.insertAdmin(adminBean);
            //查询出刚增加的管理员
            AdminBean admin = adminDaoMapper.selectAdminByAdminName(adminBean.getUserName());
            //查询成功 && roleId不为空进行角色信息添加
            if(G4Utils.isNotEmpty(admin) && G4Utils.isNotEmpty(admin.getUserId()) && G4Utils.isNotEmpty(roleId)){
                adminDaoMapper.insertAdminRole(admin.getUserId(),roleId);
            }else{
                int temp = 1 / 0;
            }
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data", null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败请联系管理员!");
            jsonDto.put("data", null);
        }
        return jsonDto;
    }
    /**
     *  分页获取管理员列表
     *   @param startTime 开始时间
     *   @param endTime 结束时间
     *  @param orderColumn 排序字段
     *  @param orderDir 排序方式
     *  @param adminBean 条件查询
     *  @author yys
     *  Description:  分页获取管理员列表
     *  @date  14:16 2018-01-09
     *  @return   List<AdminVO>
     *  throws
     */
    @Override
    public List<AdminBean> selectAdminByPage(String startTime , String endTime, String orderColumn, String orderDir, AdminBean adminBean) {
        return adminDaoMapper.selectAdminByPage(startTime ,endTime,orderColumn, orderDir, adminBean.getUserName());
    }
 /**
   *  修改管理员启用/停用状态
   *  @param adminBean
   *  @author yys
   *  Description:   修改管理员启用/停用状态
   *  @date  11:58 2018-01-10
   *  @return
   *  throws
   */
    @Override
    public void updateAdminAvailable(AdminBean adminBean) {
        adminDaoMapper.updateAdminAvailable(adminBean);
    }
    /**
     *  不同条件获取管理员列表
     *  @param adminBean
     *  @author yys
     *  description
     *  @date  17:15 2018-01-10
     *  @return  List<AdminVo> 0->>
     *  throws
     */
    @Override
    public List<AdminBean> selectAdmin(AdminBean adminBean) {
        return adminDaoMapper.selectAdmin(adminBean);
    }
 /**
   *  修改用户信息
   *  @param adminBean
   *  @param roleId  角色Id
   *  @author yys
   *  description
   *  @date  9:44 2018-01-11
   *  @return
   *  throws
   */
    @Override
    @Transactional
    public BaseDto updateAdminByUserId(AdminBean adminBean, String roleId) {
        BaseDto jsonDto = new BaseDto();
        try {
            //先修改管理员
            adminDaoMapper.updateAdminByUserId(adminBean);
            //查询出刚修改的管理员
            AdminBean admin = adminDaoMapper.selectAdminByUserId(adminBean.getUserId());
            //查询成功刚修改的管理员 && roleId不为空进行角色信息修改
            if(G4Utils.isNotEmpty(admin) && G4Utils.isNotEmpty(admin.getUserId()) && G4Utils.isNotEmpty(roleId)){
                if(G4Utils.isEmpty(roleDaoMapper.selectAdminRoleByUserId(admin.getUserId()))){
                    adminDaoMapper.insertAdminRole(admin.getUserId(),roleId);
                }else{
                    adminDaoMapper.updateAdminRole(admin.getUserId(),roleId);
                }
            }
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
            jsonDto.put("data", null);
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败请联系管理员!");
            jsonDto.put("data", null);
    }
        return jsonDto;
    }
 /**
   *  批量删除用户
   *  @param userIds
   *  @author yys
   *  Description:  批量删除用户
   *  @date  15:40 2018-01-11
   *  @return  BaseDto
   *  throws
   */
    @Override
    public BaseDto deleteAdminByUserId(String[] userIds) {
        BaseDto jsonDto = new BaseDto();
        try{
            for(String userId : userIds){
                adminDaoMapper.deleteAdminByUserId(userId);
            }
            jsonDto.put("success", new Boolean(true));
            jsonDto.put("msg", "操作成功!");
        }catch (Exception e){
            jsonDto.put("success", new Boolean(false));
            jsonDto.put("msg", "操作失败请联系管理员!");
        }
    return jsonDto;
    }
 /**
   *  根据userId获取管理员信息
   *  @param userId 用户Id
   *  @author yys
   *  Description:  根据userId获取管理员信息
   *  @date  9:56 2018-01-12
   *  @return  AdminVO
   *  throws
   */
    @Override
    public AdminBean selectAdminByUserId(String userId) {
        AdminBean adminBean = adminDaoMapper.selectAdminByUserId(userId);
        if(G4Utils.isNotEmpty(adminBean) && G4Utils.isNotEmpty(adminBean.getPassword())){
            adminBean.setPassword(G4Utils.decryptBasedDes(adminBean.getPassword()));
        }
        return adminBean;
    }
}
